﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MensUnoShop.Models;
using MensUnoShop.Areas.Admin.Models;
using MensUnoShop.Areas.Admin.Filters;

namespace MensUnoShop.Areas.Admin.Controllers
{
    [ManagerCheckFilter]
    public class TagSetupController : Controller
    {
        //
        // GET: /Admin/TagSetup/

        #region 主分類管理
        public ActionResult MainList()
        {
            Dictionary<UTag, UTagUrlAlias> tlist = TagFactory.GetManagerClassTagList(1,0);
            ViewData["action"] = TempData["action"]==null?"Add":TempData["action"];
            ViewData["bmsg"] = TempData["bmsg"] == null ? "新增主分類" : TempData["bmsg"];
            return View(tlist);
        }

        [HttpPost]
        public ActionResult MainList(FormCollection fm)
        {
            if (fm["Act"] == "Add")
            {
                if (TagFactory.AddManagerClassTag(fm["title"], fm["alias"], fm["tagalt"], fm["url"], 1, 32))
                {
                    TempData["alert"] = "新增主分類成功";
                }
                else
                    TempData["alert"] = "分類已存在或資料異常";
            }
            else if (fm["Act"] == "ChgStatus")
                TagFactory.ChgManagerTagStatus(fm["tagid"]);
            else if (fm["Act"] == "Del")
            {
                TagFactory.DelManagerTag(fm["tagid"]);
                TempData["alert"] = "標籤已刪除";
            }
            else if (fm["Act"] == "Upt")
            {
                Dictionary<UTag, UTagUrlAlias> tlist = TagFactory.GetManagerClassTag(fm["tagid"]);
                if (tlist != null)
                {
                    TempData["title"] = tlist.Keys.First().TAGtitle;
                    TempData["tagalt"] = tlist.Keys.First().TAGalt;
                    TempData["alias"] = tlist.Values.First().TUAalias;
                    TempData["url"] = tlist.Values.First().TUAurl;
                    TempData["bmsg"] = "更新主分類";
                    TempData["action"] = "Update";
                    TempData["tagid"] = tlist.Keys.First().TAGid;
                }
            }
            else if (fm["Act"] == "Update")
            {
                if (TagFactory.UptManagerClassTag(fm["tagid"], fm["title"], fm["alias"], fm["tagalt"], fm["url"]))
                {
                    TempData["alert"] = "更新主分類成功";
                }
                else
                    TempData["alert"] = "更新主分類失敗";
            }
            return RedirectToAction("MainList");
        }
        #endregion

        #region 次分類管理
        public ActionResult ChildList(int id)
        {
            ViewData["ParentTag"] = TagFactory.GetManagerClassTag(id.ToString());
            Dictionary<UTag, UTagUrlAlias> tlist = TagFactory.GetManagerClassTagList(2,id);
            ViewData["action"] = TempData["action"] == null ? "Add" : TempData["action"];
            ViewData["bmsg"] = TempData["bmsg"] == null ? "新增次分類" : TempData["bmsg"];
            return View(tlist);
        }

        [HttpPost]
        public ActionResult ChildList(FormCollection fm)
        {
            if (fm["Act"] == "Add")
            {
                if (TagFactory.AddManagerClassTag(fm["title"], fm["alias"], fm["tagalt"], fm["url"], 2, int.Parse(fm["parentid"])))
                {
                    TempData["alert"] = "新增次分類成功";
                }
                else
                    TempData["alert"] = "分類已存在或資料異常";
            }
            else if (fm["Act"] == "ChgStatus")
                TagFactory.ChgManagerTagStatus(fm["tagid"]);
            else if (fm["Act"] == "Del")
            {
                TagFactory.DelManagerTag(fm["tagid"]);
                TempData["alert"] = "分類已刪除";
            }
            else if (fm["Act"] == "Upt")
            {
                Dictionary<UTag, UTagUrlAlias> tlist = TagFactory.GetManagerClassTag(fm["tagid"]);
                if (tlist != null)
                {
                    TempData["title"] = tlist.Keys.First().TAGtitle;
                    TempData["tagalt"] = tlist.Keys.First().TAGalt;
                    TempData["alias"] = tlist.Values.First().TUAalias;
                    TempData["url"] = tlist.Values.First().TUAurl;
                    TempData["bmsg"] = "更新次分類";
                    TempData["action"] = "Update";
                    TempData["tagid"] = tlist.Keys.First().TAGid;
                }
            }
            else if (fm["Act"] == "Update")
            {
                if (TagFactory.UptManagerClassTag(fm["tagid"], fm["title"], fm["alias"], fm["tagalt"], fm["url"]))
                {
                    TempData["alert"] = "更新次分類成功";
                }
                else
                    TempData["alert"] = "更新次分類失敗";
            }
            return RedirectToAction("ChildList");
        }
        #endregion

        #region 自由標籤管理
        public ActionResult List()
        {
            List<UTag> tlist = TagFactory.GetManagerFreeTagList();
            ViewData["action"] = TempData["action"] == null ? "Add" : TempData["action"];
            ViewData["bmsg"] = TempData["bmsg"] == null ? "送出" : TempData["bmsg"];
            return View(tlist);
        }

        [HttpPost]
        public ActionResult List(FormCollection fm)
        {
            if (fm["Act"] == "Add")
            {
                if (TagFactory.AddManagerFreeTag(fm["title"].ToString()))
                    TempData["alert"] = "新增成功";
                else
                    TempData["alert"] = "標籤已存在或資料異常!";
            }
            else if (fm["Act"] == "ChgStatus")
                TagFactory.ChgManagerTagStatus(fm["tagid"]);
            else if (fm["Act"] == "Del")
            {
                TagFactory.DelManagerTag(fm["tagid"]);
                TempData["alert"] = "標籤已刪除";
            }
            else if (fm["Act"] == "Upt")
            {
                UTag tag = TagFactory.GetManagerTag(fm["tagid"],Config.TagType.FREE);
                if (tag != null)
                {
                    TempData["title"] = tag.TAGtitle;
                    TempData["bmsg"] = "更新";
                    TempData["action"] = "Update";
                    TempData["tagid"] = tag.TAGid;
                }
            }
            else if (fm["Act"] == "Update")
            {
                if (TagFactory.UptManagerTag(fm["tagid"], fm["title"]))
                {
                    TempData["alert"] = "更新標籤成功";
                }
                else
                    TempData["alert"] = "更新標籤失敗";
            }
            return RedirectToAction("List");
        }

        //挑選賣場
        public ActionResult Choice(string id)
        {
            UTag t = TagFactory.GetManagerTag(id,Config.TagType.FREE);
            ViewData["tag"] = t;
            return View();
        }

        //分配類別
        public ActionResult Appoint(string id)
        {
            UTag t = TagFactory.GetManagerTag(id, Config.TagType.FREE);
            ViewData["tag"] = t;
            return View();
        }

        
        public JsonResult api_choice(string id)
        {
            List<string[]> outlist, inlist;
            ManagerMarketFactory.GetManagerMarketChoiceList(id, out outlist, out inlist);
            return Json(new { code = "200", inlist = new { title = inlist.Select(m => m[0]), mkguid = inlist.Select(m => m[1]) }, outlist = new { title = outlist.Select(m => m[0]), mkguid = outlist.Select(m =>m[1]) } }, JsonRequestBehavior.AllowGet);
        }

        public JsonResult api_choice_search(string id, string mksn)
        {
            List<string[]> outlist, inlist;
            ManagerMarketFactory.GetManagerMarketChoiceList(id, out outlist, out inlist);
            outlist = outlist.Where(m => m[0].StartsWith(mksn,StringComparison.CurrentCultureIgnoreCase)).ToList();
            return Json(new { code = "200", inlist = new { title = inlist.Select(m => m[0]), mkguid = inlist.Select(m => m[1]) }, outlist = new { title = outlist.Select(m => m[0]), mkguid = outlist.Select(m => m[1]) } }, JsonRequestBehavior.AllowGet);
        }

        public JsonResult api_choicesetin(string id, string mklist)
        {
            if (ManagerMarketFactory.SetManagerMarketChoiceIn(id, mklist.Split(',')))
            {
                List<string[]> outlist, inlist;
                ManagerMarketFactory.GetManagerMarketChoiceList(id, out outlist, out inlist);
                return Json(new { code = "200", inlist = new { title = inlist.Select(m => m[0]), mkguid = inlist.Select(m => m[1]) }, outlist = new { title = outlist.Select(m => m[0]), mkguid = outlist.Select(m => m[1]) } }, JsonRequestBehavior.AllowGet);
            }
            else
                return Json(new { code = "500", response = "操作錯誤!!" });
        }

        public JsonResult api_choicesetout(string id, string mklist)
        {
            if (ManagerMarketFactory.SetManagerMarketChoiceOut(id, mklist.Split(',')))
            {
                List<string[]> outlist, inlist;
                ManagerMarketFactory.GetManagerMarketChoiceList(id, out outlist, out inlist);
                return Json(new { code = "200", inlist = new { title = inlist.Select(m => m[0]), mkguid = inlist.Select(m => m[1]) }, outlist = new { title = outlist.Select(m => m[0]), mkguid = outlist.Select(m => m[1]) } }, JsonRequestBehavior.AllowGet);
            }
            else
                return Json(new { code = "500", response = "操作錯誤!!" });
        }

        public JsonResult api_appoint(string id)
        {
            List<UTag> outlist, inlist;
            ManagerMarketFactory.GetManagerTagAppointList(id, out outlist, out inlist);
            return Json(new { code = "200", inlist = new { title = inlist.Select(m => m.TAGtitle), mkguid = inlist.Select(m => m.TAGid) }, outlist = new { title = outlist.Select(m => m.TAGtitle), mkguid = outlist.Select(m => m.TAGid) } }, JsonRequestBehavior.AllowGet);
        }

        public JsonResult api_appointsetin(string id, string mklist)
        {
            if (ManagerMarketFactory.SetManagerTagAppointIn(id, mklist.Split(',')))
            {
                List<UTag> outlist, inlist;
                ManagerMarketFactory.GetManagerTagAppointList(id, out outlist, out inlist);
                return Json(new { code = "200", inlist = new { title = inlist.Select(m => m.TAGtitle), mkguid = inlist.Select(m => m.TAGid) }, outlist = new { title = outlist.Select(m => m.TAGtitle), mkguid = outlist.Select(m => m.TAGid) } }, JsonRequestBehavior.AllowGet);
            }
            else
                return Json(new { code = "500", response = "操作錯誤!!" });
        }

        public JsonResult api_appointsetout(string id, string mklist)
        {
            if (ManagerMarketFactory.SetManagerTagAppointOut(id, mklist.Split(',')))
            {
                List<UTag> outlist, inlist;
                ManagerMarketFactory.GetManagerTagAppointList(id, out outlist, out inlist);
                return Json(new { code = "200", inlist = new { title = inlist.Select(m => m.TAGtitle), mkguid = inlist.Select(m => m.TAGid) }, outlist = new { title = outlist.Select(m => m.TAGtitle), mkguid = outlist.Select(m => m.TAGid) } }, JsonRequestBehavior.AllowGet);
            }
            else
                return Json(new { code = "500", response = "操作錯誤!!" });
        }
        #endregion

        #region 標籤管理
        public ActionResult TList(Config.TagType id)
        {
            List<UTag> tlist = TagFactory.GetManagerTagList(id);
            ViewData["TAGTYPE"] = Config.GetEnumDescription(id);
            ViewData["action"] = TempData["action"] == null ? "Add" : TempData["action"];
            ViewData["bmsg"] = TempData["bmsg"] == null ? "送出" : TempData["bmsg"];
            return View(tlist);
        }

        [HttpPost]
        public ActionResult TList(Config.TagType id, FormCollection fm)
        {
            if (fm["Act"] == "Add")
            {
                if (TagFactory.AddManagerTag(id, fm["title"].ToString()))
                    TempData["alert"] = "新增成功";
                else
                    TempData["alert"] = "標籤已存在或資料異常!";
            }
            else if (fm["Act"] == "ChgStatus")
                TagFactory.ChgManagerTagStatus(fm["tagid"]);
            else if (fm["Act"] == "Del")
            {
                TagFactory.DelManagerTag(fm["tagid"]);
                TempData["alert"] = "標籤已刪除";
            }
            else if (fm["Act"] == "Upt")
            {
                UTag tag = TagFactory.GetManagerTag(fm["tagid"], id);
                if (tag != null)
                {
                    TempData["title"] = tag.TAGtitle;
                    TempData["bmsg"] = "更新";
                    TempData["action"] = "Update";
                    TempData["tagid"] = tag.TAGid;
                }
            }
            else if (fm["Act"] == "Update")
            {
                if (TagFactory.UptManagerTag(fm["tagid"], fm["title"]))
                {
                    TempData["alert"] = "更新標籤成功";
                }
                else
                    TempData["alert"] = "更新標籤失敗";
            }
            return RedirectToAction("TList");
        }

        
        #endregion


    }
}
